//
//  JZHImageView.m
//  极致荟
//
//  Created by hetao on 14-5-14.
//  Copyright (c) 2014年 mac. All rights reserved.
//

#import "JZHImageView.h"

@implementation JZHImageView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}
- (void)addimageViewController:(UIViewController *)controller
{
    [self setUserInteractionEnabled:YES];
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clike:)];
    [self addGestureRecognizer:tap];
    self.controller=controller;
}

- (void)clike:(UITapGestureRecognizer *)tap
{
    UIActionSheet *actionshee=[[UIActionSheet alloc]initWithTitle:@"操作" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"照相" otherButtonTitles:@"相册", nil];
    [actionshee showInView:[UIApplication sharedApplication].keyWindow];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(buttonIndex==1)//找一张
    {
        
        if(![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]){
            [[[UIAlertView alloc]initWithTitle:@"提示" message:@"当前相册不可用" delegate:Nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil] show];
            return;
        }
        UIImagePickerController *imagecont=[[UIImagePickerController alloc]init];
        imagecont.delegate=self;
        
        [self.controller presentViewController:imagecont animated:YES completion:^{}];
        
    }else if (buttonIndex==0)//照一张
    {
        if(![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
            [[[UIAlertView alloc]initWithTitle:@"提示" message:@"当前相机不可用" delegate:Nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil] show];
            
            return;
        }
        UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
        [imagePickerController setSourceType:UIImagePickerControllerSourceTypeCamera];
        [imagePickerController setDelegate:self];
        [imagePickerController setAllowsEditing:YES];
        [self.controller presentViewController:imagePickerController animated:YES completion:^{}];
        
        
    }

}
#pragma mark 相册 代理方法实现-------------
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *img;
    NSLog(@"%@",info);
    if(picker.sourceType == UIImagePickerControllerSourceTypeCamera){
        img=[info objectForKey:UIImagePickerControllerEditedImage];
    }else {
        img=[info objectForKey:UIImagePickerControllerOriginalImage];
    }
    
    [self.controller dismissViewControllerAnimated:YES completion:^{
        self.image=img;
    }];
    
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
